package org.ecype.diego;

import java.lang.reflect.Field;

import org.ecype.util.Nulls;

import android.content.ContentValues;
import android.net.Uri;
import androidx.persistence.PersistenceException;
import androidx.persistence.access.Marker;

public class MarkerProcessor extends ActionUriProcessor {

	private ContentValues values;

	@Override
	public void setup(ContractContentProvider provider, Field field) {
		super.setup(provider, field);
		this.values = new ContentValues();
		Marker annotation = field.getAnnotation(Marker.class);
		String[] columns = annotation.columns();
		String[] values = annotation.values();
		if (columns.length != values.length) {
			throw new PersistenceException("columns and values count are mismatched, check @Marker = " + field);
		}
		for (int column = 0; column < columns.length; column++) {
			this.values.put(columns[column], values[column]);
		}
	}

	@Override
	public int update(Uri action, Uri notifyUri, ContentValues values, String selection, String[] selectionArgs) {
		Nulls.checkAreAreNull(values, selection, selectionArgs);
		Uri uri = Uris.withoutLastSegment(action);
		return provider.update(uri, notifyUri, this.values, null, null);
	}

}
